package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.AccountPrivacyDto;
import com.example.entity.vo.request.PrivacySaveVo;
import com.example.mapper.AccountPrivacyMapper;
import com.example.service.AccountPrivacyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class AccountPrivacyServiceImpl
        extends ServiceImpl<AccountPrivacyMapper, AccountPrivacyDto>
        implements AccountPrivacyService {


    @Override
    @Transactional
    public void saveAccountPrivacy(int id, PrivacySaveVo vo) {
        //读取用户隐私设置
        AccountPrivacyDto privacy
                = Optional.ofNullable(this.getById(id))
                .orElse(new AccountPrivacyDto(id));
        //很奇怪，boolean类型不叫get
        boolean status = vo.isStatus();
        //更新某个状态
        switch (vo.getType()) {
            case "phone" -> privacy.setPhone(status);
            case "email" -> privacy.setEmail(status);
            case "gender" -> privacy.setGender(status);
            case "wx" -> privacy.setWx(status);
            case "qq" -> privacy.setQq(status);
        }
        this.saveOrUpdate(privacy);
    }

    @Override
    public AccountPrivacyDto getAccountPrivacyById(int id) {
        return Optional.ofNullable(this.getById(id)).orElse(new AccountPrivacyDto(id));
    }
}
